<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #clock {
        display: flex;
      }
    </style>
  </head>
  <body>
    <div id="clock">
      <div id="elH">10</div>
      <div>:</div>
      <div id="elM">20</div>
      <div>:</div>
      <div id="elS">30</div>
    </div>
    <script>
      setInterval(() => {
        // 获取时分秒
        const d = new Date();
        const dh = d.getHours();
        const dm = d.getMinutes();
        const ds = d.getSeconds();

        // 将时分秒设置到dom，添加前导0
        elH.innerHTML = dh < 10 ? `0${dh}` : dh;
        elM.innerHTML = dm < 10 ? `0${dm}` : dm;
        elS.innerHTML = ds < 10 ? `0${ds}` : ds;
      }, 1000);
    </script>
  </body>
</html>
